package com.briup.product_source.service.Impl;

import com.briup.product_source.dao.Basic.*;
import com.briup.product_source.dao.Ext.AnalysisMapper;
import com.briup.product_source.service.AnalysisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class AnalysisServiceImpl implements AnalysisService {
    @Autowired
    private FenceHouseMapper fenceHouseMapper;
    @Autowired
    private AnimalMapper animalMapper;
    @Autowired
    private BaseAccountMapper baseAccountMapper;
    @Autowired
    private FreezerMapper freezerMapper;
    @Autowired
    private HurdlesMapper hurdlesMapper;
    @Autowired
    private AnalysisMapper analysisMapper;
    @Override
    public Map<String, List<Object>> countNum() {
        /*Map map = new HashMap<>();
        List<Integer> list = analysisMapper.countNum();
        map.put("value",list);
        return map;*/
        Map<String, Map<String, Object>> map = analysisMapper.countNumByMap();
        Set<String> names = map.keySet();
        List<Object> list = map.values().stream().map(m -> m.get("num")).collect(Collectors.toList());
        Map resultMap = new HashMap<>();
        resultMap.put("name",names);
        resultMap.put("value",list);
        return resultMap;
    }

    @Override
    public Map<String, Integer> countWeight() {
        /**
         * {30以下=10, 50以上=5, 30~50=3}
         */
        Map map = analysisMapper.countWeight();
        return map;
    }

    @Override
    public Map<String, Integer> countDisease() {
        List<Map<String, Object>> list = analysisMapper.countDisease();
        Map map = new HashMap<>();
        for (Map<String, Object> maps : list) {
            String name =(String) maps.get("name");
            Object num = maps.get("num");
            map.put(name,num);
        }
        return map;
    }

    @Override
    public Map<String, List<Object>> countSales() {
        Map map = new HashMap<>();
        List<String> names = new ArrayList<>();
        List<Integer> values = new ArrayList<>();
        for (int i = 1; i <= 12;i++) {
            names.add(i+"月");
            values.add(0);
        }
        List<Map<String, Object>> list = analysisMapper.countSales();
        for(Map<String, Object> maps : list) {
            int month = (Integer) maps.get("month");
            int num = ((BigDecimal) maps.get("num")).intValue();
            values.set(month-1,num);
        }

        map.put("name",names);
        map.put("value",values);

        return map;
    }

}
